//
//  WHRRobOrderListVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/11.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "WHRRobOrderListVC.h"
#import "WHRRobOrderListCell.h"
#import "WHRRobOrderListModel.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "UIViewController+HBD.h"
#import "KLDropDownMenu.h"
#import "LeftTableViewModel.h"
#import "CollectionCategoryModel.h"
#import "WHRCropQuestionClassifyModel.h"

@interface WHRRobOrderListVC ()<UITableViewDataSource,UITableViewDelegate,KLDropDownMenuDataSource, KLDropDownMenuDelegate>

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray<WHRRobOrderListModel *> *modelsArray;
@property (nonatomic, assign) NSInteger pageIndex; //页数


/** 第一个数组 */
@property (nonatomic, copy) NSMutableArray<WHRCropQuestionClassifyModel *> *oneArray;
/** 第二个数组 */
@property (nonatomic, copy) NSMutableArray<LeftTableViewModel *> *twoArray;
/** 第三个数组 */
@property (nonatomic, copy) NSMutableArray<CollectionCategoryModel *> *threeArray;


@property (nonatomic, assign) NSInteger currentOneIndex;
@property (nonatomic, assign) NSInteger currentTwoLeftIndex;
@property (nonatomic, assign) NSInteger currentTwoRightIndex;


/** 擅长作物ID beGoodAtCropsId */
@property (nonatomic , strong) NSString *beGoodAtCropsId;
/** fQuestionTypeId 问题分类ID */
@property (nonatomic , strong) NSString *fQuestionTypeIdId;

@end

@implementation WHRRobOrderListVC


- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupNavigationItems];
    [self creatView];
    [self setlayout];
    
    /** 导航条白色 */
    self.hbd_barTintColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    [self requestStairList];
    [self requestCategoryList];
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestList];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    
}


- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 60, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    titleLabel.text = @"诊断抢单";
    self.navigationItem.titleView = titleLabel;
    
}


#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)creatView {
    
    _tableView = [[UITableView alloc]init];
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[WHRRobOrderListCell class] forCellReuseIdentifier:[WHRRobOrderListCell reuseID]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
}

- (void)setlayout {
    
    _tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, NAVIGATION_HEIGHT)
    .widthIs(SCREEN_WIDTH)
    .heightIs(SCREEN_HEIGHT-NAVIGATION_HEIGHT);
    
    
}

#pragma mark - requestNetwork



#pragma mark - requestNetwork

/** 问题分类 */

- (void)requestCategoryList {
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"fCode"]=@"zwwtfl";
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleCrop, URL_AgricultureCircleCrop_selectDictionary) success:^(id respose) {
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.oneArray = [WHRCropQuestionClassifyModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"dictionaryModuleDetailList"]];
            
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
    } failure:^(NSError *error) {
        
    }];
  
}


/** 作物分类 */
#pragma mark - requestNetwork
/** 网络请求一级 */
- (void)requestStairList{
    
    ZX_WEAK_SELF
    
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle,  URL_AgricultureCircle_queryCftLevelOneList) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.twoArray = [LeftTableViewModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"queryAllCropCftList"]];
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
        LeftTableViewModel *model = self.twoArray[0];
        [self getCollectionData:model.classificationId];
        
    } failure:^(NSError *error) {
        
        
        
    }];
    
    
    
}
/** 作物分类 网络请求二级*/

- (void)getCollectionData:(NSString *)index {
    
    
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"classificationId"] =index;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircle, URL_AgricultureCircle_findCropByCftId) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.threeArray = [CollectionCategoryModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"cropList"]];
            
            
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
    } failure:^(NSError *error) {
        
        
        
    }];
}



/** 请求列表 */
- (void)requestList {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"beGoodAtCropsId"] = self.beGoodAtCropsId;
    params[@"beGoodAtFieldId"] = self.fQuestionTypeIdId;
    params[@"pageSize"] = @15;
    params[@"pageNum"] = @(self.pageIndex);
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectPageNotSnatchStartQuestion) success:^(id respose){
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [WHRRobOrderListModel  mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        
        if (self.pageIndex > 1) {
            self.pageIndex--;
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    }];
    
    
}


#pragma mark - tableViewDelegate & tableViewDasource



//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    KLDropDownMenu *dropDownMenu = [KLDropDownMenu dropDownMenuWithOrigin:CGPointMake(0, 0) andHeight:50];
    dropDownMenu.delegate = self;
    dropDownMenu.dataSource = self;
    return dropDownMenu;
    
}
/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    return 50;
    
}


/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[WHRRobOrderListCell class] contentViewWidth:SCREEN_WIDTH];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    WHRRobOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:[WHRRobOrderListCell reuseID] forIndexPath:indexPath];
    cell.model = self.modelsArray[indexPath.row];
    
    
  
    
    
    
    
    return cell;
    
    
}



#pragma mark - KLDropDownMenuDataSource

/** 有几列 */
- (NSInteger)numberOfColumnsInMenu:(KLDropDownMenu *)menu {
    
    return 2;
}

- (NSString *)menu:(KLDropDownMenu *)menu titleForColumn:(NSInteger)column {
    
    switch (column) {
            
        case 0:
            
            if (_oneArray.count < 1) {
                
                return @"问题分类";
            }
            
            else {
                
                return self.oneArray[_currentOneIndex].fName;
            }
            
            break;
            
        case 1:
            
            if (_threeArray.count < 1) {
                
                return @"作物分类";
            }
            
            else {
                
                return self.threeArray[_currentTwoRightIndex].cropName;
            }
            
            break;
            
       
            
        default: return @"";
            
            break;
    }
}

/** 判断是否是两级列表 */
- (BOOL)isHaveTwoTableViewInColumn:(NSInteger)column {
    
    if (column == 1) { // 地区有两个列表
        
        return YES;
    }
    
    return NO;
}

- (CGFloat)widthRatioOfLeftColumn:(NSInteger)column {
    
    if (column == 1) {
        
        return 1.0 / 3.0;
    }
    
    return 1;
}

- (NSInteger)currentLeftSelectedRow:(NSInteger)column {
    
    switch (column) {
            
        case 0:
            
            return _currentOneIndex;
            
            break;
        case 1:
            
            return _currentTwoLeftIndex;
            
            break;
        
            
        default: return 0; break;
    }
}

- (NSInteger)menu:(KLDropDownMenu *)menu numberOfMenuIndexPath:(KLMenuIndexPath *)menuIndexPath {
    
    switch (menuIndexPath.column) {
            
        case 0: return self.oneArray.count;
            
            break;
            
        case 1: {
            
            switch (menuIndexPath.menuTableType) {
                    
                case KLMenuTableLeftType:
                    
                    return self.twoArray.count;
                    
                    break;
                    
                case KLMenuTableRightType:
                    
                    return self.threeArray.count;
                    
                    break;
            }
        }
            break;
       
            
        default: return 0; break;
    }
}

- (NSString *)menu:(KLDropDownMenu *)menu titleForRowAtMenuIndexPath:(KLMenuIndexPath *)menuIndexPath {
    
    NSInteger row = menuIndexPath.row;
    
    
    WHRCropQuestionClassifyModel *model = nil;
    
    
    if (row < _oneArray.count) {
        
        model = self.oneArray[menuIndexPath.row];
        /** 获取beGoodAtFieldId */
        self.fQuestionTypeIdId = self.oneArray[menuIndexPath.row].fId;
        
    }
    
    switch (menuIndexPath.column) {
            
        case 0:
            
            return model.fName;
            
            break;
            
        case 1: {
            
            LeftTableViewModel *model1 = nil;
            CollectionCategoryModel *model2  = nil;
            
            
            if (row < _twoArray.count) {
                
                model1 = self.twoArray[menuIndexPath.row];
                
            }
            if (row < _threeArray.count) {
                
                model2 = self.threeArray[menuIndexPath.row];
                
                /** 获取擅长作物ID  */
                self.beGoodAtCropsId = self.threeArray[menuIndexPath.row].cropId;
                
            }
            
            
            switch (menuIndexPath.menuTableType) {
                    
                    
                case KLMenuTableLeftType:
                    
                    return model1.classificationName;
                    
                    break;
                    
                case KLMenuTableRightType:
                    
                    return model2.cropName;
                    
                    break;
            }
        }
            break;
      
            
        default: return @""; break;
    }
    
}

#pragma mark - KLDropDownMenuDelegate

- (void)menu:(KLDropDownMenu *)menu didSelectRowAtIndexPath:(KLMenuIndexPath *)indexPath {
    
    switch (indexPath.column) {
            
        case 0:
            
            if (self.oneArray.count < 1) {
                
                self.currentOneIndex = 0;
                
            }
            
            else {
                
                _currentOneIndex = indexPath.row;
                [self.tableView.mj_header beginRefreshing];
                
            }
            
            
            
            
            break;
        case 1: {
            
            switch (indexPath.menuTableType) {
                    
                    
                    
                case KLMenuTableLeftType:
                    
                    _currentTwoLeftIndex = indexPath.row;
                    
                    return;
                    
                    break;
                    
                case KLMenuTableRightType:
                    
                    _currentTwoRightIndex = indexPath.row;
                    
                    
                    break;
            }
        }
            
        
    }
    
    NSLog(@"选中, 菜单栏 Index: %ld, 左边 table Index: %ld, 右边 table Index: %ld", indexPath.column, indexPath.leftItem, indexPath.row);
}


#pragma mark - Lazy




- (NSMutableArray<WHRCropQuestionClassifyModel *> *)oneArray {
    
    if (!_oneArray) {
        _oneArray = [NSMutableArray<WHRCropQuestionClassifyModel *> array];
    }
    return _oneArray;
}

- (NSMutableArray<LeftTableViewModel *> *)twoArray {
    
    if (!_twoArray) {
        _twoArray = [NSMutableArray <LeftTableViewModel *> array];
    }
    
    return _twoArray;
}

- (NSMutableArray<CollectionCategoryModel *> *)threeArray {
    
    if (!_threeArray) {
        _threeArray = [NSMutableArray<CollectionCategoryModel *> array];
    }
    return _threeArray;
}




- (NSMutableArray<WHRRobOrderListModel *> *)modelsArray {
    
    if (!_modelsArray) {
        
        _modelsArray = [NSMutableArray<WHRRobOrderListModel *> array];
    }
    return _modelsArray;
    
}


@end
